<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax Post请求</title>
    <style>
        #result{
            width: 200px;
            height: 200px;
            border: 1px solid  pink;
        }
    </style>
</head>
<body>
    <div id="result"></div>
    <script>
        // 获取元素节点
        const result =document.getElementById("result");
        result.addEventListener("mouseover",function(){
            // 1.创建xhr对象
            const xhr =new XMLHttpRequest();
            // 2.创建 http 请求对象
            xhr.open("POST","http://127.0.0.1:8070/xhr-post");
            // 设置请求头
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            // 设置响应体数据的类型
            xhr.responseType='json';
            // 3. 发送请求
            xhr.send("a=100&b=200");
            // 4. 处理结果
            xhr.onreadystatechange=function(){
                if(xhr.readyState===4){
                    if(xhr.status>=200&&xhr.status<300){
                        result.innerHTML=xhr.response.name;
                    }
                }
            }
        })
    </script>
</body>
</html>